package httpHandler

import (
	baseLog "gitee.com/zaiqiang231/go-base-app/base_app/log"
	"gitee.com/zaiqiang231/lovesport-user-service/app/config"
	"gitee.com/zaiqiang231/lovesport-user-service/app/http/service"
	"gitee.com/zaiqiang231/lovesport-user-service/app/model"
	"gitee.com/zaiqiang231/lovesport-user-service/app/model/req"
	"gitee.com/zaiqiang231/lovesport-user-service/app/store"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/validator/v10"
	"net/http"
)

type SysMenuHandler struct {
}

func (*SysMenuHandler) GetMenu(c *gin.Context) {
	username := c.GetString(config.AuthIdentityKey)
	user, err := store.GetDataStore().User().Get(username)
	if err != nil {
		baseLog.Errorf("get user info fail, %v", err)
		WriteResponse(c, http.StatusUnauthorized, err.Error(), nil)
		return
	}
	menus, err := service.ServiceMenuInstance.GetMenuTree(user.AuthorityId)
	if err != nil {
		WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	if menus == nil {
		menus = []model.SysMenu{}
	}
	WriteResponse(c, 0, "success", menus)
}

func (*SysMenuHandler) GetMenuList(c *gin.Context) {
	menuList, total, err := service.ServiceMenuInstance.GetInfoList()
	if err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	WriteResponse(c, 0, "success", gin.H{
		"list":  menuList,
		"total": total,
	})
}

func (*SysMenuHandler) GetBaseMenuById(c *gin.Context) {
	var req req.GetById
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	menu, err := service.ServiceMenuInstance.GetBaseMenuById(req.ID)
	if err != nil {
		WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	WriteResponse(c, 0, "success", gin.H{
		"menu": menu,
	})
}

func (*SysMenuHandler) AddBaseMenu(c *gin.Context) {
	var menu model.SysBaseMenu
	if err := c.ShouldBindBodyWith(&menu, binding.JSON); err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	validate := validator.New()
	if err := validate.Struct(menu); err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	err := service.ServiceMenuInstance.AddBaseMenu(menu)
	if err != nil {
		WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	WriteResponse(c, 0, "success", nil)
}

func (*SysMenuHandler) DeleteBaseMenu(c *gin.Context) {
	var req req.GetById
	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	err := service.ServiceMenuInstance.DeleteBaseMenu(req.ID)
	if err != nil {
		WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	WriteResponse(c, 0, "success", nil)
}

func (*SysMenuHandler) UpdateBaseMenu(c *gin.Context) {
	var menu model.SysBaseMenu
	if err := c.ShouldBindBodyWith(&menu, binding.JSON); err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	validate := validator.New()
	if err := validate.Struct(menu); err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	err := service.ServiceMenuInstance.UpdateBaseMenu(menu)
	if err != nil {
		WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	WriteResponse(c, 0, "success", nil)
}
